<?php
namespace control;
// 文档目录
class documentCenterClass extends adIndexClass
{
    private const page  = 'documentCenter'; # 权限
    private const table = 'document_catalog'; # 数据表
    private const title = '文档中心';
    private static string $idColloection;

    /*文章入口页*/
    public function home()
    {
        self::$idColloection =  $this->staffCombination(true);
        $option['click'] = 'replaceDetail(this, \'documentCenter\')';
        $all = self::getLeftAll($option);
        $rightHtml = <<<EOT
            <div class="container-no">
                <img class="container-no-img" src="{$this->root}img/control/department-no.svg">
                <p class="center gray">请选择文章</p>
            </div>
EOT;
        $html =  componentClass::leftNav([
            'title'=>'搜索文章',
            'all'=>$all,
            'rightHtml'=>$rightHtml,
        ]);
        $html .=<<<EOT
        <script>
            var id = '{$this->get('id')}'
            $(function () {
                documentCenterChoice(id)
            });
        </script>
EOT;
        //返回
        return $this->homeFun($html);
    }

    /**
     * 获取员工数据
     *
     */
    public static function getLeftAll($data)
    {
        $orderBy = ' ORDER BY list ASC ';
        // 条件
        $data['where'] = $data['where'] ?? "pid='0'";
        // 点击事件
        $data['click'] = $data['click'] ?? 'replaceDetail(this, \'documentCatalog\')';
        // 结果
        $result = find(['table'=>self::table, 'where'=> $data['where'].$orderBy ]);
        $style = ($data['where'] == "pid='0'") ? '' : ' class="child-node"';
        $temp = "<ul {$style} {$data['where']}>";
        // 角标
        $arrow = arrow;
	    $lastone = lastone;
        $textcontent_ico = "<span class='textcontent_ico'></span>";
        if ($result['array']) {
            foreach ($result['array'] as $value) {
                //  判断该层是否有内容存在
                $document = self::getDocument([
                    'table' => 'document',
                    'id' => $value['id'],
                    'click' => $data['click'],
                ]);
                // 判断下级是否存在
                $data['where'] = " pid='{$value['id']}' ";
                $has = query(self::table, $data['where']);
                if ($has['id']) {
                    $temp .="<li data-id='{$value['id']}' data-type='catalog' onclick=\"ulScal(this, '{$value['id']}')\" title='{$value['name']}'>
                                {$arrow}
                                <span class='textcontent'>{$textcontent_ico}{$value['name']}</span>
                            </li>";
                    $temp .= self::getLeftAll($data);
                } else {
                    if ($document) {
                        $temp .="<li data-id='{$value['id']}' data-type='catalog' onclick=\"ulScal(this, '{$value['id']}')\" title='{$value['name']}'>
                                    {$arrow}
                                    <span class='textcontent'>{$textcontent_ico}{$value['name']}</span>
                                </li>";
                    } else {
                        $temp .="<li data-id='{$value['id']}' data-type='catalog' title='{$value['name']}'>
                                    {$lastone}
                                    <span class='textcontent'>{$textcontent_ico}{$value['name']}</span>
                                </li>";
                    }
                }
                $temp .= $document ?? '';
            }
        }
        $temp .= '</ul>';
        return $temp;
    }

    /**
     * 左侧搜索框
     *
     * @return string
     */
    public function getContainerLeftJson():string
    {
        $option['click'] = 'replaceDetail(this, \'documentCenter\')';
        $json['html'] = self::getLeftAll($option);
        return json_encode($json, 320);
    }

    /**
     * 查询
     */
    public static function getDocument($data)
    {
        $temp = "";
        $idColloection = self::$idColloection;
        $result = find(['table'=> $data['table'], 'field'=> 'id, title', 'where'=> " `dcid` = '{$data['id']}' AND `state` = '已发布' AND ((concat(',',replace(selector,';',','),',') regexp concat(',',replace('{$idColloection}',',',',|,'),','))>0  OR `stid` = '".self::$stid."') order by publicTime desc"]);
        if ($result['array']) {
            $temp .= '<ul class="child-node" pid="'.$data['id'].'">';
            foreach ($result['array'] as $value) {
                $temp .= "<li data-id='{$value['id']}' data-type='document' onclick=\"{$data['click']}\" title='{$value['title']}' class='li-color'><span class='textcontent'>$value[title]</span></li>";
            }
            $temp .= '</ul>';
        }
        return $temp;
    }


    public function detail()
    {
        $info = query('document', " id = '".$this->post('id')."' ");
        if (!$this->power(self::page, "see")) {
            $json['warn'] = "你没有查看的权限";
        } elseif (empty($this->post('id'))) {
            $json['warn'] = "文章ID为空";
        } elseif (empty($info['id'])) {
            $json['warn'] = "未找到此文章";
        } else {
            $data = [
                "id" => $info['id'],
                "title" => $info['title'],
            ];
            //按钮
            //生成表单
            $data['html'] = $this->tableShow([
                 0  => [
                    [
                        '<h3>'.$info['title'].'</h3>',
                    ],
                    [
                        "发布人"  => self::findStaff($info['publicStid']),
                        "发布时间" => self::null($info['publicTime']),
                    ],
                    [
                        self::htmlDecode($info['content'], false),
                    ],
                    [
                        '附件' => (new fileUploadClass())->showFile($info['id'], '文档管理')
                    ]
                ]
            ]);
            //生成页面
            $json['warn'] = 2;
            $json['html'] = '<div class="document-body">'.$data['html'].'</div>';
        }
        //返回
        return json_encode($json, 320);
    }
}
